/*
*  Copyright 2019-2023 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.system.rest;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import lombok.RequiredArgsConstructor;
import me.zhengjie.annotation.Log;
import me.zhengjie.modules.system.domain.Clazzandteacher;
import me.zhengjie.modules.system.domain.Teacher;
import me.zhengjie.modules.system.domain.vo.ClazzandteacherQueryCriteria;
import me.zhengjie.modules.system.domain.vo.CourseteacherQueryCriteria;
import me.zhengjie.modules.system.service.ClazzandteacherService;
import me.zhengjie.utils.PageResult;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
* @author shitou
* @date 2025-02-03
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "班级教师管理")
@RequestMapping("/api/clazzandteacher")
public class ClazzandteacherController {

    private final ClazzandteacherService clazzandteacherService;

//    @Log("导出数据")
//    @ApiOperation("导出数据")
//    @GetMapping(value = "/download")
//    @PreAuthorize("@el.check('clazzandteacher:list')")
//    public void exportClazzandteacher(HttpServletResponse response, ClazzandteacherQueryCriteria criteria) throws IOException {
//        clazzandteacherService.download(clazzandteacherService.queryAll(criteria), response);
//    }

    @GetMapping
    @Log("查询班级教师")
    @ApiOperation("查询班级教师")
//    @PreAuthorize("@el.check('clazzandteacher:list')")
    public ResponseEntity<List<Teacher>> queryClazzandteacher(ClazzandteacherQueryCriteria criteria){
        return new ResponseEntity<>(clazzandteacherService.queryAll(criteria),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增班级教师")
    @ApiOperation("新增班级教师")
//    @PreAuthorize("@el.check('clazzandteacher:add')")
    public ResponseEntity<Object> createClazzandteacher(@Validated @RequestBody ClazzandteacherQueryCriteria criteriad){

        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("clazzId",criteriad.getClazzId());
        List<Clazzandteacher> clazzandteacherList = clazzandteacherService.list(queryWrapper);
        for(Clazzandteacher clazzandteacher:clazzandteacherList){
            clazzandteacherService.removeById(clazzandteacher.getId());
        }
        for(Integer item:criteriad.getTeacherIds()){
            Clazzandteacher clazzandteacher = new Clazzandteacher();
            clazzandteacher.setClazzId(criteriad.getClazzId());
            clazzandteacher.setTeacherId(item);
            clazzandteacherService.create(clazzandteacher);
        }

        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改班级教师")
    @ApiOperation("修改班级教师")
//    @PreAuthorize("@el.check('clazzandteacher:edit')")
    public ResponseEntity<Object> updateClazzandteacher(@Validated @RequestBody Clazzandteacher resources){
        clazzandteacherService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除班级教师")
    @ApiOperation("删除班级教师")
//    @PreAuthorize("@el.check('clazzandteacher:del')")
    public ResponseEntity<Object> deleteClazzandteacher(@RequestBody List<Integer> ids) {
        clazzandteacherService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}